package top.haichi.webpaper.Interceptors;


import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import top.haichi.webpaper.entity.Result;
import top.haichi.webpaper.utils.InterceptorUtils;
import top.haichi.webpaper.utils.JwtUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Component
@Slf4j

/**
 * 文件上传拦截器
 */
public class FileUploadInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        if(request.getHeader("token")==null||request.getHeader("token").length()<1){
            InterceptorUtils.result(Result.error("未登录"),response);
            return false;
        }
        if(JwtUtils.getTokenParam(request,"status").equals("teacher")){
            return true;
        }
        if(JwtUtils.getTokenParam(request,"id").equals(request.getParameter("id"))){
            return true;
        }

        log.error("id校验失败");
        InterceptorUtils.result(Result.tokenError(),response);
        return false;
    }
}
